<template>
  <div>
    <el-form>
      <el-form-item label="名称">
        <el-input v-model="resourceData.name"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="resourceData.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="$emit('cancel')">取消</el-button>
        <el-button type="primary"
                   @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { saveOrUpdate } from '@/services/resource-category'

export default Vue.extend({
  name: 'CreateOrEditResource',
  props: {
    resource: {
      type: [Object]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      resourceData: {
        sort: '',
        name: ''
      },
      resourceCategories: []
    }
  },

  created() {
    // 如果是编辑操作，则根据角色 ID 加载展示角色信息
    if (this.isEdit) {
      this.resourceData = this.resource
    }
  },

  methods: {
    async onSubmit() {
      await saveOrUpdate(this.resourceData)
      this.$message.success('操作成功')
      this.$emit('success')
    }
  }
})
</script>

<style lang="scss" scoped></style>
